﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class pages_EditData_Restore : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["flag"] == null)
            {
                Response.Redirect("../../Login.aspx");
            }
        }
    }
    protected void btnRestore_Click(object sender, EventArgs e)
    {
        string path = this.fileShow.PostedFile.FileName; //获得备份路径及数据库名称
        //这行代码为关键代码，使用该SQL语句执行数据还原命令
        string cmdtxt = "backup log db_Material to disk='" + path + "'";
        cmdtxt += "USE master DECLARE tb CURSOR LOCAL FOR SELECT 'Kill '+ CAST(Spid AS VARCHAR) FROM master.dbo.sysprocesses";
        cmdtxt += " WHERE dbid=DB_ID('db_Material') DECLARE @s nvarchar(1000) OPEN tb FETCH tb INTO @s";
        cmdtxt += " WHILE @@FETCH_STATUS = 0 BEGIN EXEC (@s) FETCH tb INTO @s END CLOSE tb DEALLOCATE tb";
        cmdtxt += "  RESTORE DATABASE db_Material FROM disk='" + path + "'";
        cmdtxt += "use db_Material";

        SqlConnection con = DB.GetCon();
        if (con.State == ConnectionState.Open)
        {
            con.Close();
            con.Dispose();
        }
        try
        {
            con.Open();
            SqlCommand sqlcom = new SqlCommand(cmdtxt, con);
            sqlcom.ExecuteNonQuery();
            Response.Write("<script language=javascript>alert('还原数据成功！')</script>");
            con = DB.GetCon();
            con.Open();
            SqlCommand com = new SqlCommand("select * from tb_Material",con);
            com.ExecuteNonQuery();
            com.Dispose();
            con.Close();
            con.Dispose();
        }
        catch (Exception oe)
        {
        }       
    }
}
